package com.study.d012Components.audio;

import java.io.File;
import java.net.URL;

import javafx.animation.Animation;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.media.AudioClip;
import javafx.stage.Stage;

/** 音频播放   播放小的音频资源
*@description
*@author xtayhame
*date 2020年3月18日
*version
*since v1.0
*/
public class AudioClipMain extends Application{
	public static void main(String[] args) {
		launch(args);
	}
	@Override
	public void start(Stage stage) throws Exception {
		AnchorPane anchorPane = new AnchorPane();

		HBox hBox = new HBox(20);

		Button playButton = new Button("播放");
		Button stopButton = new Button("暂停");

		//记载 将军令  音乐
		URL url = FXMLLoader.getDefaultClassLoader().getResource("com/study/d012Components/audio/2.mp3");
		System.out.println(url.toExternalForm());

		AudioClip clip = new AudioClip(url.toExternalForm());
		//设置平衡  -1:左声道   1:右声道
		clip.setBalance(0);

		//播放次数，  Animation.INDEFINITE 无限次
		clip.setCycleCount(Animation.INDEFINITE);

		//甚至音量大小
		clip.setVolume(0.5);

		//设置播放速度    1是正常播放
		clip.setRate(0.1);

		System.out.println("加载后");
		hBox.getChildren().addAll(playButton,stopButton);
		anchorPane.getChildren().add(hBox);
		stage.setScene(new Scene(anchorPane, 400, 300));
		stage.show();

		playButton.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				clip.play();
			}
		});
		stopButton.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				clip.stop();
			}
		});
	}
}
